# 指定cmake的最低版本
cmake_minimum_required(VERSION 3.20)

# 项目名称
project(cmake_if)

#[[
if(<condition>)
    <commands>
elseif(<condition>)
    <commands>
else()
    <commands>
endif()
]]

#[[
if(<常量>)
如果常量是 1, ON, YES, TRUE, Y 或 非零数，则为真
如果常量是 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, 空字符串，或以-NOTFOUND结尾，则为假
不区分大小写
]]
if(1)
    message("1 is true")
endif()

if(0)
    message("0 is false")
endif()

if(OFF)
    message("OFF is false")
elseif(NO)
    message("NO is false")
else()
    message("OFF/NO is false")
endif()

if()
    message("empty is true")
else()
    message("empty is false")
endif()

if(-1)
    message("-1 is true")
endif()

if(0.1)
    message("0.1 is true")
endif()

if(-NOTFOUND)
    message("-NOTFOUND is true")
else()
    message("-NOTFOUND is false")
endif()

#[[
if(<variable>)
如果变量的值为非假常量，则为真，否则为假
变量如果未定义，则为假
环境变量 if(ENV{some_var}) 为假
]]
if(VAR_NOT_DEF)
    message("VAR_NOT_DEF is true")
else()
    message("VAR_NOT_DEF is false")
endif()

set(VER1_TRUE TRUE)
if(VER1_TRUE)
    message("VER1_TRUE is true")
endif()

#[[
if(<string>)
带引号的字符串计算为 false，除非字符串的值是 相应的常量
]]
if("TRUE")
    message("string_TRUE is true")
endif()

if("test")
else()
    message("string_test is false")
endif()

if("123")
else()
    message("string_123 is false")
endif()

message("================================================================")

#[[
逻辑运算符
if(NOT <condition>) # 非

if(<condition1> AND <condition2>) # 和

if(<condition1> OR <condition2>) # 或

if(<condition1> AND (<condition2> OR <condition3>)) # 多条件
]]
set(VAR_OFF OFF)
if(NOT VAR_OFF)
    message("NOT VAR_OFF")
endif()

if(TRUE AND ON)
    message("TRUE AND ON is true")
endif()

if(TRUE AND OFF)
else()
    message("TRUE AND OFF is false")
endif()

if(TRUE OR OFF)
    message("TRUE OR OFF is true")
endif()

if(TRUE AND (OFF OR ON))
    message("TRUE AND (OFF OR ON) is true")
endif()

message("================================================================")
#[[
一元判断：
    EXISTS
    COMMAND 
    DEFINED # 判断变量是否被定义
二元判断：
    EQUAL # 数字相等
    EQUAL, LESS, LESS_EQUAL, GREATER, GREATER_EQUAL
    STREQUAL,  # 字符串相等
    MATCHES
    if(<variable|string> MATCHES regex) # 正则表达式
]]
set(VER_DEF OFF)
if(DEFINED VER_DEF) # 判断变量是否被定义
    message("VER_DEF is defined")
endif()

if(DEFINED VER_NOT_DEF) # 判断变量是否被定义
    message("VER_NOT_DEF is not defined")
endif()

set(VAR1 123)
if(VAR1 EQUAL "123") # 判断数字是否相等
    message("VAR1 EQUAL 123")
endif()

set(VAR2 "test")
if(VAR2 STREQUAL "test") # 判断字符串是否相等
    message("VAR2 STREQUAL test")
endif()

# 正则表达式匹配
if("abcd1234" MATCHES "^[a-z]+$")
    message("\"abcd1234\" MATCHES \"^[a-z]+$\"")
else()
    message("\"abcd1234\" NOT MATCHES \"^[a-z]+$\"")
endif()